package symbolTable.seniorST;

import stackAndQueue.Queue;
import symbolTable.primaryST.SequentialSearchST;


public class SeparateChainingHashST <Key, Value>{
    private int N;
    private int M;
    private SequentialSearchST<Key, Value>[] st;
    public SeparateChainingHashST(){
        this(997);
    }
    public SeparateChainingHashST(int M){
        this.M = M;
        st = (SequentialSearchST<Key, Value>[])new SequentialSearchST[M];
        for (int i=0;i<M;i++)
            st[i] = new SequentialSearchST<>();
    }

    private int hash(Key key){
        return (key.hashCode() & 0x7fffffff) % M;
    }
    public void put(Key key, Value value){
        st[hash(key)].put(key, value);
    }
    public Value get(Key key){
        Value value = st[hash(key)].get(key);
        return value;
    }
    public void delete(Key key){
        st[hash(key)].delete(key);
    }
    public Iterable keys(){
        Queue<Key> queue = new Queue<>();
        SequentialSearchST<Key,Value> se;
        for (int i=0; i<M; i++){
            se = st[i];
            Iterable<Key> keys = se.keys();
            for (Key key:keys){
                queue.enqueue(key);
            }
        }
        return queue;
    }
}
